function clean() {
    $('#answerField').parent().removeClass('has-error has-feedback').find('span').remove();
}

function validate() {
    clean();
    var validator = {valid: false};
    var answerField = $('#answerField').val();
    if (!answerField) {
        $('#answerField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#answerField');
        return validator;
    }
    if (answerField.length > 700) {
        $('#answerField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Post must have 1-700 characters!</span>').insertAfter('#answerField');
        return validator;
    }

    validator["answerField"] = answerField;
    validator["valid"] = true;
    return validator;
}

function addNewTopicClick(eventObject) {
    var validator = validate();
    if (validator["valid"]) {
        sendNewPost(topic_id, validator["answerField"], function (data) {
            if (data["success"] !== -1) {
                getPostWithId(data["success"], function (pos) {
                    var html = 
                            '<div class="well well-lg">' +
                                '<div class="row">' +
                                    '<div class="col-xs-9">' +
                                        pos["message"] +
                                    '</div>' +
                                    '<div class="col-xs-3">' +
                                        pos["created_date"] +
                                    '</div>' +
                                '</div>' +
                                '<div class="row">' +
                                    '<div class="col-xs-12">' +
                                        pos["name_user"] +
                                    '</div>' +
                                '</div>' +
                            '</div>';
                    $("#postsDiv").append(html);
                    $('#answerField').val("");
                });
            } else {
            }
        });
    }
}

